package com.raul.noticias.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raul.noticias.beens.Noticia;
import com.raul.noticias.database.factory.DAOFactory;
import com.raul.noticias.interfaces.InterfaceNoticiasDAO;

/**
 * Servlet implementation class Conexion
 */
public class Noticias extends HttpServlet {

	private static DAOFactory factory = null;

	private static InterfaceNoticiasDAO nDao = null;
	private static final long serialVersionUID = -3566790248122214310L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Noticias() {
		super();
		factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
		nDao = factory.getInterfaceNoticiasDAO();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Noticia noticia = nDao.getNoticia(request.getParameter("id"));
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html>");
		out.println("<html lang='es'>");
		out.println("	<head>");
		out.println("		<title>Servlet</title>");
		out.println("	</head>");
		out.println("	<body>");
		out.println("		<h1>" + noticia.toString() + "</h1>");
		out.println("	<body>");

		request.setAttribute("noticia", noticia);

		RequestDispatcher dispatcher = this.getServletContext()
				.getRequestDispatcher("/noticia.jsp");

		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
